// 版权声明，Epic Games 公司保留所有权利

#pragma once

#include "System/GameplayTagStack.h"
#include "Templates/SubclassOf.h"

#include "LyraInventoryItemInstance.generated.h"

class FLifetimeProperty;

class ULyraInventoryItemDefinition;
class ULyraInventoryItemFragment;
struct FFrame;
struct FGameplayTag;

/**
 * Lyra库存物品实例类
 */
UCLASS(BlueprintType)
class ULyraInventoryItemInstance : public UObject
{
	GENERATED_BODY()

public:
	ULyraInventoryItemInstance(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get()); // 构造函数

	//~UObject interface
	virtual bool IsSupportedForNetworking() const override { return true; } // 支持网络复制
	//~End of UObject interface

	/**
	 * 添加属性标签堆栈
	 * @param Tag 游戏标签
	 * @param StackCount 堆叠数量
	 */
	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Inventory)
	void AddStatTagStack(FGameplayTag Tag, int32 StackCount);

	/**
	 * 移除属性标签堆栈
	 * @param Tag 游戏标签
	 * @param StackCount 堆叠数量
	 */
	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category= Inventory)
	void RemoveStatTagStack(FGameplayTag Tag, int32 StackCount);

	/**
	 * 获取属性标签堆叠数量
	 * @param Tag 游戏标签
	 * @return 堆叠数量
	 */
	UFUNCTION(BlueprintCallable, Category=Inventory)
	int32 GetStatTagStackCount(FGameplayTag Tag) const;

	/**
	 * 检查是否具有属性标签
	 * @param Tag 游戏标签
	 * @return 是否包含标签
	 */
	UFUNCTION(BlueprintCallable, Category=Inventory)
	bool HasStatTag(FGameplayTag Tag) const;

	/**
	 * 获取物品定义类
	 * @return 物品定义类
	 */
	TSubclassOf<ULyraInventoryItemDefinition> GetItemDef() const
	{
		return ItemDef;
	}

	/**
	 * 根据片段类查找片段
	 * @param FragmentClass 片段类
	 * @return 找到的片段
	 */
	UFUNCTION(BlueprintCallable, BlueprintPure=false, meta=(DeterminesOutputType=FragmentClass))
	const ULyraInventoryItemFragment* FindFragmentByClass(TSubclassOf<ULyraInventoryItemFragment> FragmentClass) const;

	/**
	 * 根据片段类查找片段（模板版本）
	 * @return 找到的片段
	 */
	template <typename ResultClass>
	const ResultClass* FindFragmentByClass() const
	{
		return (ResultClass*)FindFragmentByClass(ResultClass::StaticClass());
	}

private:
#if UE_WITH_IRIS
	/** 注册所有复制片段 */
	virtual void RegisterReplicationFragments(UE::Net::FFragmentRegistrationContext& Context, UE::Net::EFragmentRegistrationFlags RegistrationFlags) override;
#endif // UE_WITH_IRIS

	/**
	 * 设置物品定义类
	 * @param InDef 物品定义类
	 */
	void SetItemDef(TSubclassOf<ULyraInventoryItemDefinition> InDef);

	friend struct FLyraInventoryList; // 友元声明

private:
	UPROPERTY(Replicated)
	FGameplayTagStackContainer StatTags; // 属性标签堆栈容器

	UPROPERTY(Replicated)
	TSubclassOf<ULyraInventoryItemDefinition> ItemDef; // 物品定义类
};